package com.heima.comment.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.comment.service.CommentService;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.model.comment.dtos.CommentDto;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.comment.dtos.CommentSaveDto;
import com.heima.model.comment.pojos.Comment;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import com.heima.utils.common.AppJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import thread.AppThreadLocalUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;
 //评论内容检测
    @Autowired
    private GreenTextScan greenTextScan;

    /**
     * 评论内容审核及保存
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {
        Comment comment = new Comment();
        try {
            //参数校验
            if (dto==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

            }
            //验证是否登录
            ApUser user = AppThreadLocalUtil.getUser();
            if (user==null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
            //判断评论字数
            if (dto.getContent().length()>140){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论内容不能超过140字");
            }

            //评论内容做垃圾检测
            Map map = greenTextScan.greeTextScan(dto.getContent());
            if (!map.get("suggestion").equals("pass")){
                return ResponseResult.errorResult(501,"评论内容包含违法内容!");
            }

            comment.setEntryId(dto.getArticleId());
            comment.setContent(dto.getContent());
            comment.setAddress("蜀西路精诚国际1号");
            comment.setAuthorName(user.getName());
            comment.setCreatedTime(new Date());
        } catch (Exception e) {
            throw new RuntimeException("评论失败,请重试!");
        }

        mongoTemplate.save(comment);
        return ResponseResult.okResult(200,"评论成功!");
    }

    @Override
    public ResponseResult loadAll(CommentSaveDto dto) {


        Long articleId = dto.getArticleId();
        Date minDate = dto.getMinDate();    //.andOperator(Criteria.where("createdTime").lt(minDate))
        Query query = Query.query(Criteria.where("entryId").is(articleId).and("createdTime").lt(minDate))
                .with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
//        if (commentList.size()==0){
//            throw new RuntimeException("查询评论为空!!");
//        }
        ResponseResult responseResult = new PageResponseResult(1,10,commentList.size());
        responseResult.setData(commentList);

        return responseResult;
    }

    @Override
    public ResponseResult likeComment(CommentLikeDto dto) {

        Comment comment = mongoTemplate.findById(dto.getCommentId(), Comment.class);

        Integer likes = comment.getLikes();

        if (dto.getOperation() == 0) {


            comment.setLikes(likes + 1);
        } else {
            comment.setLikes(likes-1);
        }

        mongoTemplate.save(comment);

        return ResponseResult.okResult(likes);
    }
}
